package com.lkedu.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lkedu.entity.CourseBean;
import com.lkedu.entity.UserBean;
import com.lkedu.service.UserService;
import com.lkedu.utils.MailUtils;
import com.sun.crypto.provider.RSACipher;




@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
    
    
	public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("------------register----------");
		String username=request.getParameter("username");
		String password=request.getParameter("pwd");
		String phone=request.getParameter("phone");
		String email=request.getParameter("email");
		String sex=request.getParameter("sex");
		String birthday=request.getParameter("birthday");
		
		UserBean userBean=new UserBean();
		userBean.setUsername(username);
		userBean.setPassword(password);
		userBean.setPhone(phone);
		userBean.setEmail(email);
		userBean.setSex(sex);
		try {
			userBean.setBirthday((new SimpleDateFormat("yyyy-MM-dd")).parse(birthday));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		//调用业务
		UserService userService=new UserService();
		boolean isSuccess=userService.register(userBean);
		
		//根据业务结果跳转不同的页面
		if(isSuccess) {
			response.sendRedirect("login.jsp");
		}else {
			response.sendRedirect("register.jsp");
		}
	}
	
	public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("------------login----------");
		String username=request.getParameter("loginName");
		String password=request.getParameter("loginPwd");
		UserService userService=new UserService();
		if(username.equals("admin")) {
			request.getSession().setAttribute("loginuser", "admin");
			response.sendRedirect("admin.jsp");
		}
		else {
			boolean isLogin=userService.login(username, password);
			
			if(isLogin) {
				//存储用户登录信息
				request.getSession().setAttribute("loginuser", username);
				response.sendRedirect("index.jsp");
			}else {
				request.getSession().setAttribute("msg", "用户名或密码错误！");
				response.sendRedirect("login.jsp");
			}
		}
	}
	
	public void checkUsername(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		System.out.println("------------checkUsername----------");
		String username=request.getParameter("username");
		UserService userService=new UserService();
		boolean result=userService.checkUsername(username);
		String json="{\"result\":\""+(result?1:0)+"\"}";
		response.getWriter().write(json);
	}
	
	public void reset_check(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("------------reset_check----------");
		String resetPwdName=request.getParameter("resetPwdName");
		String restEmail=request.getParameter("restEmail");
		UserService userService=new UserService();
		UserBean u=userService.resetCheck(resetPwdName,restEmail);
		if(u!=null) {
			request.getSession().setAttribute("uid", u.getUid());
			MailUtils mailUtils = new MailUtils();
			try {
				mailUtils.sendMail(restEmail, "http://localhost:8080/lkedu/resetPwd.jsp");
			} catch (AddressException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MessagingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			request.getSession().setAttribute("msg", "请留意邮箱信息");
			response.sendRedirect("login.jsp");
		}
		else {
			request.getSession().setAttribute("msg", "用户名或邮箱不存在");
			response.sendRedirect("reset.jsp");
		}
	}
	
	public void reset_changPwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("------------reset_changPwd----------");
		String newPwd=request.getParameter("newPwd");
		String uid=(String)request.getSession().getAttribute("uid");
		UserService userService=new UserService();
		userService.resetCheckPwd(uid,newPwd);
		response.sendRedirect("login.jsp");
	}
	
	public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("------------logout----------");
		request.getSession().removeAttribute("loginuser");
		response.sendRedirect("login.jsp");
	}
	
	public void myCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("------------myCourse----------");
		UserService userService=new UserService();
		String uname=request.getParameter("uname");
		List<CourseBean> courseBeans=userService.myCourse(uname);
		request.setAttribute("courseBeans", courseBeans);
		request.getRequestDispatcher("user.jsp").forward(request, response);
	}
	
	public void delCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("------------delCourse----------");
		UserService userService=new UserService();
		String uname=request.getParameter("uname");
		String cid=request.getParameter("cid");
		int result=userService.delCourse(uname,cid);
		response.sendRedirect("UserServlet?method=myCourse&uname="+uname);
	}
	public void addCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("------------addCourse----------");
		UserService userService=new UserService();
		String uname=request.getParameter("uname");
		String cid=request.getParameter("cid");
		int result=userService.addCourse(uname,cid);
		response.sendRedirect("CourseServlet?method=search&keywords=");
	}

}
